package com.huixuebao.exchange.service.Impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.exchange.dto.CoursePolicyDTO;
import com.huixuebao.exchange.dto.PurposeDTO;
import com.huixuebao.exchange.entity.CourseEntity;
import com.huixuebao.exchange.entity.InsuredEntity;
import com.huixuebao.exchange.entity.PurposeEntity;
import com.huixuebao.exchange.mapper.CourseMapper;
import com.huixuebao.exchange.mapper.InsuredMapper;
import com.huixuebao.exchange.mapper.PurPoseMapper;
import com.huixuebao.exchange.service.InsuredService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class InsuredServiceImpl implements InsuredService {

    @Autowired
    InsuredMapper insuredMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    PurPoseMapper purPoseMapper;


    @Override
    public ApiResult getPolicyList(String userId,String settleState) {
        ApiResult apiResult = new ApiResult();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("userId", userId);
            map.put("settleState", settleState);
            List<CoursePolicyDTO> dtoList = insuredMapper.selectOrderPolicy(map);
            apiResult.setData(dtoList);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("获取投保订单信息成功！！");
        } catch (Exception e) {
            log.info("查询投保订单信息失败!!");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("查询投保订单信息失败！！");
        }
        return apiResult;
    }

    @Override
    public ApiResult addPurpose(PurposeDTO purposeDTO) {
        ApiResult apiResult = new ApiResult();
        try {
            String mechanismId = purposeDTO.getMechanismId();
            String mechanismName = purposeDTO.getMechanismName();
            String courseName = purposeDTO.getCourseName();
            String coursePrice = purposeDTO.getCoursePrice();
            String courseStartTime = purposeDTO.getCourseStartTime();
            String courseEndTime = purposeDTO.getCourseEndTime();
            String contractUrl = purposeDTO.getContractUrl();
            String holderId = purposeDTO.getHolderId();
            String insuredId = purposeDTO.getInsuredId();
            String premium = purposeDTO.getPremium();
            String totalPremium = purposeDTO.getTotalPremium();

            if(StrUtil.isNotEmpty(mechanismId) && StrUtil.isNotEmpty(insuredId)){
                PurposeEntity purposeEntity = new PurposeEntity();
                purposeEntity.setId(IdUtil.simpleUUID());
                purposeEntity.setMechanismId(mechanismId);
                purposeEntity.setMechanismName(mechanismName);
                purposeEntity.setCourseName(courseName);
                purposeEntity.setCoursePrice(coursePrice);
                purposeEntity.setCourseStartTime(courseStartTime);
                purposeEntity.setCourseEndTime(courseEndTime);
                purposeEntity.setContractUrl(contractUrl);
                purposeEntity.setHolderId(holderId);
                purposeEntity.setInsuredId(insuredId);
                purposeEntity.setPremium(premium);
                purposeEntity.setCreateTime(String.valueOf(new DateTime()));
                purposeEntity.setTotalPremium(totalPremium);
                purPoseMapper.insert(purposeEntity);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("添加团险订单信息成功！！");
            }else {
                log.info("必填字段不能为空！！！");
                apiResult.setSuccess(false);
                apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
                apiResult.setMessage("添加团险订单信息失败！！");
            }
        }catch (Exception e){
            log.info("增添团购订单信息异常！！！");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("添加团险订单信息异常！！");
        }
        return apiResult;
    }
}
